<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="../common.jsp"%>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="${ctx}/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="${ctx}/css/public.css" media="all" />
</head>
<body class="childrenBody">
<form class="layui-form">
    <blockquote class="layui-elem-quote quoteBox">
        <form class="layui-form">
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" class="layui-input userPhone" placeholder="手机号" />
                </div>

                <div class="layui-input-inline">
                    <input type="text" class="layui-input userName" placeholder="用户名称" />
                </div>
                <a class="layui-btn search_btn" data-type="reload">搜索</a>
                <a class="layui-btn layui-btn-normal add_btn" data-type="add">新增</a>
                <a class="layui-btn layui-btn-radius layui-btn-normal " <%--href="${requestScope.excelLodeUrl}" --%> id="excelLodeUrl" >下载模板</a>
                <button type="button" class="layui-btn layui-btn-danger" id="Import"><i class="layui-icon">&#xe67c;</i>导入</button>
        </form>
    </blockquote>
    <table id="adminList" lay-filter="adminList"></table>
</form>
<!--操作-->
<script type="text/html" id="adminListBar">
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn layui-btn-xs" lay-event="changePassword">重置密码</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script type="text/javascript" src="${ctx}/layui/layui.js"></script>
<script>
    layui.use(['form', 'layer', 'table', 'laytpl', 'laydate','upload'], function () {
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery,
            laytpl = layui.laytpl,
            table = layui.table,
            laydate = layui.laydate;
           var e = layui.$;
        var upload = layui.upload;


        //分页列表
        var tableIns = table.render({
            elem: '#adminList',
            url: '${ctx}/users/page',
            cellMinWidth: 95,
            page: true,
            height: "full-125",
            loading: true,
            id: "adminListTable",
            request: {
                pageName: 'pageNumber' //页码的参数名称，默认：page
                ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            response: {
                statusName: 'code' //规定数据状态的字段名称，默认：code
                ,statusCode: 10200 //规定成功的状态码，默认：0
                ,msgName: 'message' //规定状态信息的字段名称，默认：msg
                ,countName: 'total' //规定数据总数的字段名称，默认：count
                ,dataName: 'records' //规定数据列表的字段名称，默认：data
            },
            cols: [[
                {field: 'userName', title: '用户名称', minWidth: 100, align: "center"},
                {field: 'userPhone', title: '用户手机号', minWidth: 100, align: "center"},
                {field: 'buyNum', title: '最多购买车位数', minWidth: 100, align: "center"},
                {field: 'gmtCreate', title: '创建时间', minWidth: 100, align: "center"},
                {title: '操作', width: 360, templet: '#adminListBar', fixed: "right", align: "center"}
            ]]
        });


        //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
        $(".search_btn").on("click", function () {
            tableIns.reload({
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: { //设定异步数据接口的额外参数，任意设
                    userName: $(".userName").val(),
                    userPhone: $(".userPhone").val()
                }
            })
        });


        var loginLoading1;
        var Loading;
        //文件上传
        var uploadInst = upload.render({
            elem: '#Import'
            , url: "${ctx}/users/importData"
            ,accept: 'file'
            ,size:1024*8
            ,multiple:true
            ,exts :'xls|xlsx'
            ,acceptMime:'.xls,.xlsx'
            , before: function (obj) {
                //loginLoading1 = top.layer.msg('正在导入经销商信息，请稍候...', {icon: 16, time: false, shade: 0.8});
                Loading = layer.msg( '<div style="width:430px" lay-filter="my-progress" class="layui-progress layui-progress-big" lay-showPercent="true">\n' +
                    '  <div class="layui-progress-bar layui-bg-blue" lay-percent=""><span class="right" id="my-progress"></span></div>\n' +
                    '</div>', {icon: 16, time: false, shade: 0.2,area: 'auto',maxWidth: '500'});
                obj.preview(function (index, file, result) {});
                //创建定时任务
                // window.progressInterval = window.setInterval(window.may_progress,100);
                // $("#my-progress").css("margin-right","0px");
                // var progress= "<div style='width:"+width/1.5+"  margin: 0 auto;' lay-filter='myprogress' class='layui-progress layui-progress-big layui-layer-nobg' lay-showPercent='yes'>"+
                //     "<div class='layui-progress-bar layui-bg-blue' lay-percent='0%'><span style='margin-right: 20px;' id='ss'>50%</span></div>"+
                //     "</div>";
                // layer.open({
                //     title: false,
                //     type: 1,
                //     skin:'layui-layer-nobg',
                //     move: false,
                //     offset: '250px',
                //     shade:0.3,
                //     resize : false,
                //     but:false,
                //     area: [width / 1.2 + "px", 18 + "px"],
                //     content: progress //这里content是一个普通的String
                //     ,cancel: function(){
                //         //右上角关闭回调
                //         return false ;//开启该代码可禁止点击该按钮关闭
                //     }
                // });
                // element.progress('myprogress', 50+"%");
            }
            , done: function (data) {
                //上传成功
                if (data.code == 10200) {
                    top.layer.msg(data.message, {icon: 6});
                    layer.close(Loading);
                    location.reload();
                }
                //如果上传失败
                if (data.code != 10200) {
                    top.layer.msg(data.message, {icon: 5});
                    layer.close(Loading);
                    return false;
                }
            }
            , error: function () {
                return top.layer.msg("导入用户信息失败！", {icon: 5});
            }
        });






        e("#excelLodeUrl").on("click", function () {
            window.location.href='${ctx}/json/users.xlsx'
        });




        //重置管理员密码
        function changePassword (userId) {
            layui.layer.confirm("该用户密码将被重置为123456,确认吗？", {
                icon: 3,
                skin: 'larry-green',
                title: "重置密码提示",
                offset: '200px',
                closeBtn: 0,
                skin: 'layui-layer-molv',
                anim: Math.ceil(Math.random() * 6),
                btn: ['确定', '取消']
            },function (index) {
                $.ajax({
                    url: '${ctx}/users/restPassword',
                    data: {userId:userId},
                    method: 'post',
                    dataType: 'json',
                    success: function (res) {
                        if (res.code == 10200) {
                            layer.msg("操作成功", {icon: 6});
                            tableIns.reload();
                        }else {
                            layer.msg(res.message, {icon: 5});
                        }
                    },
                    error: function () {
                        layer.msg("系统繁忙,请稍后再试!", {icon: 5});
                    }
                });
                layui.layer.close(index);
            })
        }


        //删除管理员
        function del (userId) {
            layui.layer.confirm("该用户将被删除,确认吗？", {
                icon: 3,
                skin: 'larry-green',
                title: "删除提示",
                offset: '200px',
                closeBtn: 0,
                skin: 'layui-layer-molv',
                anim: Math.ceil(Math.random() * 6),
                btn: ['确定', '取消']
            },function (index) {
                $.ajax({
                    url: '${ctx}/users/deleteById',
                    data: {userId:userId},
                    method: 'post',
                    dataType: 'json',
                    success: function (res) {
                        if (res.code == 10200) {
                            layer.msg("操作成功", {icon: 6});
                            tableIns.reload();
                        }else {
                            layer.msg(res.message, {icon: 5});
                        }
                    },
                    error: function () {
                        layer.msg("系统繁忙,请稍后再试!", {icon: 5});
                    }
                });
                layui.layer.close(index);
            })
        }

        //新增
        $(".add_btn").click(function () {
            toEdit();
        });

        //编辑
        function toEdit(userId) {
            var index = layui.layer.open({
                title: "编辑用户",
                type: 2,
                content: "${ctx}/page/toEditUsers?userId="+userId,
                success: function () {
                    setTimeout(function () {
                        layui.layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    }, 500)
                }
            })
            layui.layer.full(index);
            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
            $(window).on("resize", function () {
                layui.layer.full(index);
            })
        }


        e("#excelLodeUrl").on("click", function () {
            window.location.href='${ctx}/json/users.xlsx'
        });

        //列表操作
        table.on('tool(adminList)', function (obj) {
            var layEvent = obj.event,
                //当前行数据
                data = obj.data;
            if (layEvent === 'changePassword') {
                changePassword(data.userId)
            }
            if (layEvent === 'del') {
                del(data.userId)
            }
            if (layEvent === 'edit') {
                toEdit(data.userId)
            }
        });
    })
</script>
</body>
</html>
